var DATA = {};

app.controller('forumListCtrl', ['$scope', function ($scope) {
	DATA.ids = "";	//根据ID去重复
	var needNewPagination = true;
	
	var cateIds = Utils.getUrlParam("cateIds");
	$("#cate-name").text(Utils.getUrlParam("cateName"));
	
	refreshHotList(1);	//以热贴为主，新贴为辅
	
	function refreshHotList(pageNo) {
		var param = {
			cateIds: cateIds,
			startTime: null,
			endTime: null,
			pageNo: pageNo,
			pageCount: 15,
		};
		AQ.callAQJSON("getHotEntry.do", param, function(result) {
			refreshNewList(pageNo);	//以热贴为主，新贴为辅
			var list = result.data;
			var data = [];
			for (var i=0; i<list.length; i++) {
				var forumEntry = list[i];
				if(DATA.ids.indexOf(forumEntry.id) >= 0) {
					continue;
				} else {
					DATA.ids = DATA.ids + forumEntry.id + ",";	//根据ID去重复
				}
				forumEntry.cateName = DIC.ForumCate(forumEntry.categoryId);
				data.push(forumEntry);
			}
			$scope.$apply(function () {
				$scope.hotListData = data;
				if(pageNo == 1 && needNewPagination) {	//创建分页
					var num_entries = result.size;
					Utils.newPagination("Pagination", num_entries, function(page_index, jq) {
						if(page_index == 0 && needNewPagination) {
							needNewPagination = false;
							return;
						}
						DATA.ids = "";	//根据ID去重复
						refreshHotList(page_index+1);
					}, 15);
				}
			});
		}, function(err, code) {
			Utils.msgDialog(err + "["+ code +"]");
		});
	}
	
	function refreshNewList(pageNo) {
		var param = {
			cateIds: cateIds,
			pageNo: pageNo,
			pageCount: 15,
		};
		AQ.callAQJSON("getNewEntry.do", param, function(result) {
			var list = result.data;
			var data = [];
			for (var i=0; i<list.length; i++) {
				var forumEntry = list[i];
				if(DATA.ids.indexOf(forumEntry.id) >= 0) {
					continue;
				} else {
					DATA.ids = DATA.ids + forumEntry.id + ",";	//根据ID去重复
				}
				forumEntry.cateName = DIC.ForumCate(forumEntry.categoryId);
				data.push(forumEntry);
			}
			$scope.$apply(function () {
				$scope.newListData = data;
			});
		}, function(err, code) {
			Utils.msgDialog(err + "["+ code +"]");
		});
	}
	
	
}]);



$(document).ready(function() {
	
	//点击搜索
    $("#search-forum").click(function() {
        var searchWords = $.trim($(".fm-txt").val());
		if(searchWords != "") {
			location.replace("forum-search.html?searchWords="+ searchWords);
		}
    });
	//点回车搜索
	$(".fm-txt").keydown(function(e) {
        if(e.keyCode == "13") {
			var searchWords = $.trim($(".fm-txt").val());
			if(searchWords != "") {
				location.replace("forum-search.html?searchWords="+ searchWords);
			}
		}
    });
	
});
